<!DOCTYPE html>
<html class="layui-bg-white">
<head th:include="include :: header"></head>
<body>
<div class="layui-form" style="padding:20px;">
    <input id="id" name="id" th:value="${information.id}" type="hidden">
    <input type="hidden" id="subjectName" name="subjectName" th:value="${information.subjectName}">
    <input type="hidden" id="typeName" name="typeName" th:value="${information.typeName}">
    <input type="hidden" id="content" name="content">
    <div class="layui-form-item">
        <label class="layui-form-label">科目名称：</label>
        <div class="layui-input-block">
            <select lay-filter="subjectId" id="subjectId" name="subjectId" class="form-control chosen-select" lay-search>
                <option value="">--请选择--</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">资讯标题：</label>
        <div class="layui-input-block">
            <input type="text" id="title" name="title" placeholder="请输入资讯标题" autocomplete="off" class="layui-input" th:value="${information.title}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>资讯类型：</label>
        <div class="layui-input-block">
            <input type="hidden" th:value="${information.typeId}">
            <select lay-filter="typeId" id="typeId" name="typeId" class="form-control chosen-select" lay-search lay-verify="required">
                <option value="">--请选择--</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>是否发布：</label>
        <div class="layui-input-block">
            <input type="checkbox" lay-skin="switch" lay-filter="switchState" lay-text="是|否" th:checked="${information.isPublish=='1'?true:false}">
            <input type="hidden" name="isPublish" id="isPublish" th:value="${information.isPublish}">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">资讯内容：</label>
        <div class="layui-input-block">
            <textarea id="content1" class="layui-textarea" placeholder="请输入资讯内容" th:text="${information.content}"></textarea>
        </div>
    </div>
    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="LAY-operate-submit" id="LAY-operate-submit" value="确认">
    </div>
</div>
<div th:include="include :: footer"></div>
<script type="text/javascript">
    var information = {};
    layui.config({
        base: ctx + '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index'         //主入口模块
    }).use(['index', 'form', 'layedit'], function () {
        var $ = layui.$, form = layui.form, layedit = layui.layedit;

        form.verify(commonJs.form.verify);

        form.on('switch(switchState)', function (data) {
            if (data.elem.checked == true) {
                $('#isPublish').val('1');
            } else {
                $('#isPublish').val('0');
            }
        });

        //初始化一二级类别
        function getDictList(type, parentId, target) {
            type = type || '';
            parentId = parentId || '';
            var url = ctx + "/manage/dict/dict/getDictList";
            var data = {'type': type, 'parentId': parentId};
            var success = function (result) {
                if (result.code == 0) {
                    $("#" + target).html("<option value=''>--请选择--</option>");
                    var data = result.data;
                    $.each(data, function (index, item) {
                        $("#" + target).append("<option  value=" + item.id + ">" + item.name + "</option>");
                    });
                    $("#" + target).val($("#" + target).prev("input").val());
                    form.render();
                }
            };
            commonJs.openAjax(url, data, success, null, false);
        }

        getDictList('information_type', null, 'typeId');

        form.on('select(typeId)', function (data) {
            var value = data.value || '';
            var text = '';
            if (value != '') {
                text = data.elem[data.elem.selectedIndex].text || '';
            }
            $('#typeName').val(text);
        });

        function getDataList(target) {
            var url = ctx + "/manage/subject/subject/list";
            var data = {};
            var success = function (result) {
                if (result.code == 0) {
                    $("#" + target).html("<option value=''>--请选择--</option>");
                    var data = result.data;
                    $.each(data, function (index, item) {
                        $("#" + target).append("<option  value=" + item.id + ">" + item.name + "</option>");
                    });
                    form.render();
                }
            };
            commonJs.openAjax(url, data, success, null, false, "GET");
        }

        getDataList('subjectId');

        form.on('select(subjectId)', function (data) {
            var value = data.value || '';
            var text = '';
            if (value != '') {
                text = data.elem[data.elem.selectedIndex].text || '';
            }
            $('#subjectName').val(text);
        });

        //初始和获取富文本的内容
        layedit.set({
            uploadImage: {
                url: ctx + '/manage/file/file/layedit' //接口url
            }
        });
        var content = layedit.build('content1');

        //初始化form的数据
        information.initFormData = function () {
            $('#content').val(layedit.getContent(content));
            console.log($('#content').val());
        };
    })
</script>
</body>
</html>